package boxe.timer.manager;

/**
 * Control the timer and the action from the menu.
 * 
 * @author Julien Perriard
 * @date 18.05.2010
 *
 */

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.AbstractButton;
import javax.swing.JOptionPane;

import boxe.timer.RunProcess;
import boxe.timer.env.Propertie;
import boxe.timer.view.MainWindow;
import boxe.timer.view.ModeManagerView;

public class TimerController implements ActionListener {
	/*
	 * main window
	 */
	private MainWindow mainWindow;
	/*
	 * Timer status
	 */
	private String status = null;
	/*
	 * thread for timer 
	 */
	private static Thread thread;
	
	/*
	 * Timer process
	 */
	private RunProcess runProcess;
	
	/*
	 * mode manager
	 */
	private ModeManager modeManager;
	
	/**s
	 * Constructor
	 * 
	 * @param modeManager
	 */
	public TimerController(ModeManager modeManager){
		/*
		 * init main view
		 */
		mainWindow = new MainWindow(this);	
		/*
		 * init timer
		 */
		status = Propertie.ST_STOP;
		
		/*
		 * init mode manager
		 */
		this.modeManager = modeManager;
		modeManager.setMainWindow(mainWindow);
		/*
		 * init thread
		 */
		thread  = null;
	}

	/**
	 * Get main window
	 * 
	 * @return
	 */
	public MainWindow getWindow() {
		return mainWindow;	
	}

	/**
	 * Action listener
	 * 
	 */
	public void actionPerformed(ActionEvent e) {
		String item = String.valueOf(((AbstractButton) e.getSource())
				.getActionCommand());
		
		if(modeManager.getListMode() == null || modeManager.getListMode().isEmpty()) {
			 
		}
		/*
		 * Close window
		 */
		if (item.equals(Propertie.CLOSE)){
			System.exit(0);
		}
		
		/*
		 * Start the timer
		 */
		else if (item.equals(Propertie.ST_START)){
			    
			    /*
			     * kill the thread if previous is stop
			     */
				if(status.equals(Propertie.ST_STOP)) {
					thread = null;
				}
				
				status = Propertie.ST_START;
				
				/*
				 * start thread timer
				 */
				if (thread == null) {
					runProcess = new RunProcess(this, modeManager.getCurrentMode());
			    	thread = new Thread(runProcess);
			    	thread.start();
			}
		}
		
		/*
		 * hold the timer
		 * 
		 */
		else if (item.equals(Propertie.ST_HOLD)){
			if(status.equals(Propertie.ST_START)) {
				status = Propertie.ST_HOLD;
			}
		}
		
		/*
		 * stop timer
		 */
		else if (item.equals(Propertie.ST_STOP)){
			stopTimer();
		}
		
		/*
		 * Display about dialog
		 */
		else if (item.equals(Propertie.ABOUT)){
			JOptionPane.showMessageDialog(mainWindow,
				    "Boxe Timer for the Masta Kick-Boxer 'Kevin'\n" +
				    		"by Julien. Good training!",
				    "About",
				    JOptionPane.INFORMATION_MESSAGE);
		}
		
		/*
		 * display mode manager window
		 */
		else if (item.equals(Propertie.MODE_MANAGER)){
			modeManager.getView().setVisible(true);
		}
		
		/*
		 * save current config as xml
		 */
	    else if (item.equals(Propertie.SAVE)) {
	    	ConfigManager conf = new ConfigManager();
	    	conf.saveConfig(modeManager.getListMode());
	    } 
		
	    /*
	     * load config from xml file
	     */
	    else if (item.equals(Propertie.LOAD)) {
	    	ConfigManager conf = new ConfigManager();
	    	modeManager.setListMode(conf.loadConfig());
	    }
	  }
	
	/**
	 * Stop the thread timer
	 */
	public void stopTimer() {
		status = Propertie.ST_STOP;
		Thread myThread = thread;
		thread = null;
		if(myThread != null) {
			myThread.interrupt();
		}
	}

	/**
	 * Display the number of round
	 * @param nbr
	 */
	public void setNbrRound(int nbr){
		if (nbr<10){
			this.getWindow().lblRound.setText("0"+Integer.toString(nbr));
		}
		else{
			this.getWindow().lblRound.setText(Integer.toString(nbr));
		}
	}
	
	/**
	 * Display dialog error
	 */
	public void messageError(String msg){
		JOptionPane.showMessageDialog(mainWindow, msg, "Warning", JOptionPane.ERROR_MESSAGE);
	}
	
	/**
	 * Set Background Color timer
	 * @param background
	 */
	public void setColor(Color background){
		getWindow().panTimer.setBackground(background);
	}
		
	/**
	 * Setter for time clock
	 * @param clock
	 */
	public void setTimeClock(long clock){
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");	
		getWindow().lblTimer.setText(sdf.format(new Date(clock)));
	}
	
	/**
	 * Setter for time clock
	 * @param clock
	 * @param background
	 */
	public void setTimeClock(long clock, Color background){
		setTimeClock(clock);
		getWindow().panTimer.setBackground(background);
	}

	/**
	 * Setter for status
	 * @param status
	 */
	public void setStatus(String status) {
		this.status = status;	
	}

	/**
	 * Getter for status
	 * @return
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * Is timer running
	 * @return
	 */
	public boolean isStart() {
		return status.equals(Propertie.ST_START);
	}
	
	/**
	 * Is timer holdig
	 * @return
	 */
	public boolean isHold() {
		return status.equals(Propertie.ST_HOLD);
	}
	
	/**
	 * Is timer stop
	 * @return
	 */
	public boolean isStop() {
		return status.equals(Propertie.ST_STOP);
	}
	
	/**
	 * Update timer view 
	 */
	public void changeView(Color background, int nbRun, long current){
  		setTimeClock(current, background);
  		setNbrRound(nbRun);	
	}

	/**
	 * Getter for modeManager
	 * @return
	 */
	public ModeManager getModeManager() {
		return modeManager;
	}

	/**
	 * Setter for modeManager
	 * @param modeManager
	 */
	public void setModeManager(ModeManager modeManager) {
		this.modeManager = modeManager;
	}
}
